package cn.chinaflame.pagodas.service;

import cn.chinaflame.pagodas.entity.PaProductDetail;
import cn.chinaflame.pagodas.repository.PaProductDetailRepository;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;


/**
 * 商品详情service
 *
 * @author created by wwt
 * @date 2018年1月6日 下午2:35:17
 */
@Service
public class PaProductDetailService {


    private static final Logger log = LoggerFactory.getLogger(PaProductDetailService.class);

    @Autowired
    private PaProductDetailRepository paProductDetailRepository;

    /**
     * 查询商品区域列表
     *
     * @param pageIndex
     * @param pageSize
     * @param id
     * @param name
     * @return
     */
    public List<PaProductDetail> queryList(Integer pageIndex, Integer pageSize, String id, String productId) {
        log.info("查询商品详情列表id:{},productId:{}", id, productId);
        List<PaProductDetail> entityList = paProductDetailRepository.findAll(new Specification<PaProductDetail>() {
            @Override
            public Predicate toPredicate(Root<PaProductDetail> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = Lists.newArrayList();
                if (!Strings.isNullOrEmpty(id)) {
                    list.add(cb.equal(root.get("id").as(String.class), id));
                }
                if (!Strings.isNullOrEmpty(productId)) {
                    list.add(cb.equal(root.get("productId").as(String.class), productId));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        });
        List<PaProductDetail> itemList = Lists.newArrayList();
        for (PaProductDetail item : entityList) {

            itemList.add(item);
        }
        return itemList;
    }
}
